package com.anysoft.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 将Map转化为Properties
 * @author yyduan
 * @since 1.6.8.12
 *
 * @version 1.6.14.1 [20210310 duanyy] <br>
 * - Properties更改为接口，改从Properties.Abstract继承 <br>
 */
public class MapProperties extends Properties.Abstract{

	protected Map<String,Object> map = null;
	
	public MapProperties(Map<String,Object> map,Properties parent){
		super("default",parent);
		
		this.map = map;
	}
	
	@Override
	protected void _SetValue(String _name, String _value) {
		if (map != null){
			map.put(_name, _value);
		}
	}

	@Override
	protected String _GetValue(String _name) {
		if (map != null){
			Object found = map.get(_name);
			return found == null? null : found.toString();
		}
		return null;
	}

	@Override
	public void Clear() {
		if (map != null){
			map.clear();
		}
	}

	@Override
	public void list(List<Pair<String,String>> result){
		if (map != null) {
			Iterator<Map.Entry<String, Object>> iter = map.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry<String, Object> entry = iter.next();
				Object obj = entry.getValue();
				if (obj != null) {
					result.add(new Pair.Default<String, String>(entry.getKey(), obj.toString()));
				}
			}
		}
	}

	public static class StringValue extends Properties.Abstract{
		protected Map<String,String> map = null;

		public StringValue(Map<String,String> map,Properties parent){
			super("default",parent);

			this.map = map;
		}

		@Override
		protected void _SetValue(String _name, String _value) {
			if (map != null){
				map.put(_name, _value);
			}
		}

		public void list(List<Pair<String,String>> result){
			if (map != null) {
				Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
				while (iter.hasNext()) {
					Map.Entry<String, String> entry = iter.next();
					result.add(new Pair.Default<String, String>(entry.getKey(), entry.getValue()));
				}
			}
		}

		@Override
		protected String _GetValue(String _name) {
			if (map != null){
				Object found = map.get(_name);
				return found == null? null : found.toString();
			}
			return null;
		}

		@Override
		public void Clear() {
			if (map != null){
				map.clear();
			}
		}

	}
}